home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / BoxMaker++ / BoxMaker++ ƒ / preferences.cp < prev    next >
Encoding:
Text File  |  1995-01-20  |  2.5 KB  |  107 lines  |  [TEXT/KAHL]

  1. #include <Files.h>
  2. #include <Finder.h>
  3. #include <Folders.h>
  4.  
  5. #include "preferences.h"
  6.  
  7. template<class C> preferences<C>::preferences( Str255 fName, const C &defaults,
  8.     OSType fileCreator) : iFileCreator( fileCreator)
  9. {
  10.     //
  11.     // if the preferences file exists already, use the defaults specified in it
  12.     // instead of the defaults passed to us.
  13.     //
  14.     long foundDirID;
  15.     short foundVRefnum;
  16.  
  17.     short theFile;
  18.     long numtoread = sizeof( C);
  19.     
  20.     (void)FindFolder( kOnSystemDisk, 'pref', kCreateFolder, &foundVRefnum, &foundDirID);
  21.     (void)FSMakeFSSpec( foundVRefnum, foundDirID, fName, &theFileSpec);
  22.     
  23.     if( FSpOpenDF( &theFileSpec, fsCurPerm, &theFile) == noErr)
  24.     {
  25.         (void)FSRead( theFile, &numtoread, (Ptr)&theDefaults);
  26.         (void)FSClose( theFile);
  27.     } else {
  28.         theDefaults = defaults;
  29.     }
  30.     //
  31.     // Start with default values:
  32.     //
  33.     *(C*)this = theDefaults;
  34. }
  35.  
  36. template<class C> preferences<C>::~preferences()
  37. {
  38.     if( defaultsChanged())
  39.     {
  40.         long foundDirID;
  41.         short foundVRefnum;
  42.         //
  43.         // Create the preferences file, in case it doesn't exist yet
  44.         // This will fail if the file exists already, but we don't care
  45.         // about that.
  46.         //
  47.         short theFile;
  48.         FSpCreateResFile( &theFileSpec, iFileCreator, 'pref', 0);
  49.         if( ResError() == 0)
  50.         {
  51.             //
  52.             // First time through: put 'STR ' resource in,
  53.             // if it can be found in the application.
  54.             //
  55.             const short applResFile = CurResFile();
  56.             theFile = FSpOpenResFile( &theFileSpec, fsWrPerm);
  57.             if( theFile != -1)
  58.             {
  59.                 UseResFile( applResFile);
  60.                 Handle theRes = Get1Resource( 'STR ', kPreferencesInfo);
  61.                 if( theRes != 0)
  62.                 {
  63.                     DetachResource( theRes);
  64.                     UseResFile( theFile);
  65.                     AddResource( theRes, 'STR ', kPreferencesInfo, "\p");
  66.                     ReleaseResource( theRes);    // AddResource made it a resource again
  67.                 }
  68.                 CloseResFile( theFile);
  69.             }
  70.             //
  71.             // Set 'Name Locked' flag of preferences file:
  72.             //
  73.             FInfo theInfo;
  74.             if( FSpGetFInfo( &theFileSpec, &theInfo) == noErr)
  75.             {
  76.                 theInfo.fdFlags |= kNameLocked;
  77.                 (void)FSpSetFInfo( &theFileSpec, &theInfo);
  78.             }
  79.         }
  80.         if( FSpOpenDF( &theFileSpec, fsWrPerm, &theFile) == noErr)
  81.         {
  82.             long numtowrite = sizeof( C);
  83.             (void)FSWrite( theFile, &numtowrite, (Ptr)(C*)this);
  84.             FSClose( theFile);
  85.         }
  86.     }
  87. }
  88.  
  89. template<class C> Boolean preferences<C>::defaultsChanged() const
  90. {
  91.     const char *one   = (const char *)this;
  92.     const char *other = (const char *)&theDefaults;
  93.     const int numBytes = sizeof( C);
  94.     Boolean result = false;
  95.     
  96.     for( int i = 0; i < numBytes; i++)
  97.     {
  98.         if( *one++ != *other++)
  99.         {
  100.             result = true;
  101.             break;
  102.         }
  103.     }
  104.     return result;
  105. }
  106.  
  107.